package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.domain.NewsInfoDomain;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

/**
 * 信息CURD的Dao
 * @author lj
 *
 */
@Repository
public class NewsInfoDao extends BaseDao{
	private static final Log logger=LogFactory.getLog(UserInfoDao.class);
	
	/**
	 * 根据条件分页查询信息
	 * @param dgm 分页参数
	 * @param uqp 信息查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,NewsInfoDomain uqp){
		logger.debug("查询信息");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from newsinfo where 1=1");
		if(uqp.getNewsTitle()!=null&&!"".equals(uqp.getNewsTitle())){
			sql.append(" and newsTitle like ?");
			params.add("%"+uqp.getNewsTitle()+"%");
		}
		if(uqp.getNewsContent()!=null&&!"".equals(uqp.getNewsContent())){
			sql.append(" and newsContent like ?");
			params.add("%"+uqp.getNewsContent()+"%");
		}
		if(uqp.getNewsType()!=null&&!"".equals(uqp.getNewsType())&&!"-1".equals(uqp.getNewsType())){
			sql.append(" and newsType = ?");
			params.add(uqp.getNewsType());
		}
		if(uqp.getStatus()!=-1){
			sql.append(" and status=?");
			params.add(uqp.getStatus());
		}
			
		//判断是否排序
		if(dgm.getSort()!=null&&!"".equals(dgm.getSort()))
			sql.append(" order by ").append(dgm.getSort()).append(" ").append(dgm.getOrder());
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, NewsInfoDomain.class);
	}
	
	public List<NewsInfoDomain> getNewsById(String id){
		String sql="select * from newsinfo where newsid=?";
		List<NewsInfoDomain> list= JdbcUtil.queryForList(this, NewsInfoDomain.class, sql, id);
		if(list.size()>0)
			return list;
		return null;
	}
	
	public List<NewsInfoDomain> getNews(String type){
		String sql="select * from newsinfo where newsType=? and status=2";
		List<NewsInfoDomain> list= JdbcUtil.queryForList(this, NewsInfoDomain.class, sql,type);
		if(list.size()>0)
			return list;
		return null;
	}
	
	public List<NewsInfoDomain> queryNews(){
		String sql="select * from newsinfo";
		List<NewsInfoDomain> list= JdbcUtil.queryForList(this, NewsInfoDomain.class, sql);
		if(list.size()>0)
			return list;
		return null;
	}
	
	public List<NewsInfoDomain> getCompNews(String id){
		String sql="select * from newsinfo where newsId=?";
		List<NewsInfoDomain> list= JdbcUtil.queryForList(this, NewsInfoDomain.class, sql,id);
		if(list.size()>0)
			return list;
		return null;
	}
	
	/**
	 * 增加一个信息
	 * @param NewsInfoDomain
	 */
	public void add(NewsInfoDomain newsInfoDomain){
		super.addObj(newsInfoDomain);
	}
	
	public void update(NewsInfoDomain newsInfoDomain){
		super.updateObj(newsInfoDomain, "newsId");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 */
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from newsinfo where newsid in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
	/**
	 * 批量删除
	 * @param ids
	 */
	public void updateStatus(String[] ids,String status){
		StringBuilder delSql=new StringBuilder();
		delSql.append("update  newsinfo set status ='"+status+"' where newsid in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		
		this.getJdbcTemplate().update(delSql.toString(),ids);
	}
	
}
